{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llm_vllm_data import valid_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def industry_from_prompt(prompt):\n",
    "    name = re.search(\"请仔细阅读企业信息：(.*?)的经营范围\", prompt)\n",
    "    return name.group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_label(label):\n",
    "    result = label[\"result\"]\n",
    "    try:\n",
    "        label = re.search(\"\\[\\'(.*?)\\'\\]\", result).group()\n",
    "        label = eval(label)\n",
    "    except:\n",
    "        return result\n",
    "    return label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_array = []\n",
    "for key, label in valid_data.items():\n",
    "    industry_name = industry_from_prompt(key)\n",
    "    cls = extract_label(label)\n",
    "    llm_array.append([industry_name, label[\"result\"], cls])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_df = pd.DataFrame(llm_array, columns=[\"企业名称\", \"大模型分类原因\", \"企业类别\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_df.to_csv(\"data/llm_predict.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2741, 3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 拼接\n",
    "\n",
    "# 原始的所有氢能企业\n",
    "raw_df = pd.read_csv(\"../data/氢_all_col.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "merge_df = pd.merge(\n",
    "    left=raw_df,\n",
    "    right=llm_df,\n",
    "    how=\"left\",\n",
    "    on=\"企业名称\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "merge_df.to_csv(\"data/氢能企业分类.csv\", encoding=\"utf-8-sig\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/home/jie/shells/ali.py\", line 3, in <module>\n",
      "    import aligo\n",
      "ModuleNotFoundError: No module named 'aligo'\n"
     ]
    }
   ],
   "source": [
    "!python ~/shells/ali.py -up -f -r tmp/ -l data/氢能企业分类.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.1.-1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
